<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input v-model="dataForm.content" placeholder="内容"></el-input>
      </el-form-item>
      <el-form-item label="时间" prop="subtime">
        <el-input v-model="dataForm.subtime" placeholder="时间"></el-input>
      </el-form-item>
       <el-form-item label="单位" prop="subunit">
        <el-input v-model="dataForm.subunit" placeholder="单位"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-input v-model="dataForm.type" placeholder="类型"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
// import { fileUpload } from '@/utils';
//import QRCode from 'qrcode'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        content: "",
        subtime: "",
        subunit: "",
        type: ""
      },
      dataRule: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
       content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
       subtime: [
          { required: true, message: "时间不能为空", trigger: "blur" }
        ],
        subunit: [{ required: true, message: "单位不能为空", trigger: "blur" }],
        type: [{ required: true, message: "类型不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    //  customUpload(imageUrl){
    //     fileUpload(imageUrl).then(response => {
    //        this.dataForm.imageUrl.push(response.data.url) ;
    //       })
    // },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/rencai/abilitytalentrecentannouncements/delete`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.supply.name;
              this.dataForm.content = data.supply.content;
              this.dataForm.subunit= data.supply.subunit; 
              this.dataForm.subtime= data.supply.subtime; 
              this.dataForm.type = data.supply.type;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/rencai/abilitytalentrecentannouncements/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              content:this.dataForm.content,
              subunit:this.dataForm.subunit,
              subtime:this.dataForm.subtime,
              type:this.dataForm.type,
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
